package com.fzhucc.modules.businessCard.service.impl;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.businessCard.domain.BusinessCardPre;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardPreDto;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardPreQueryCriteria;
import com.fzhucc.modules.businessCard.repository.BusinessCardPreRepository;
import com.fzhucc.modules.businessCard.service.BusinessCardPreService;
import com.fzhucc.modules.businessCard.service.mapstruct.BusinessCardPreMapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yyg
 * @description 服务实现
 * @date 2022-12-21
 **/
@Service
@RequiredArgsConstructor
public class BusinessCardPreServiceImpl implements BusinessCardPreService {

    private final BusinessCardPreRepository businessCardPreRepository;
    private final BusinessCardPreMapper businessCardPreMapper;

    @Override
    public Map<String, Object> queryAll(BusinessCardPreQueryCriteria criteria, Pageable pageable) {
        Page<BusinessCardPre> page = businessCardPreRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(businessCardPreMapper::toDto));
    }

    @Override
    public List<BusinessCardPreDto> queryAll(BusinessCardPreQueryCriteria criteria) {
        return businessCardPreMapper.toDto(businessCardPreRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public BusinessCardPreDto findById(Integer id) {
        BusinessCardPre businessCardPre = businessCardPreRepository.findById(id).orElseGet(BusinessCardPre::new);
        ValidationUtil.isNull(businessCardPre.getId(), "BusinessCardPre", "id", id);
        return businessCardPreMapper.toDto(businessCardPre);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessCardPreDto create(BusinessCardPre resources) {
        String phone = resources.getPhone();
        List<BusinessCardPre> list = businessCardPreRepository.findByPhone(phone);
        if (list != null && list.size() > 0) {
            throw new BadRequestException("已存在此手机号");
        }
        return businessCardPreMapper.toDto(businessCardPreRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BusinessCardPre resources) {
        BusinessCardPre businessCardPre = businessCardPreRepository.findById(resources.getId()).orElseGet(BusinessCardPre::new);
        String phone = resources.getPhone();
        if (!businessCardPre.getPhone().equals(phone)){
            List<BusinessCardPre> list = businessCardPreRepository.findByPhone(phone);
            if (list != null && list.size() > 0) {
                throw new BadRequestException("已存在此手机号");
            }
        }
        ValidationUtil.isNull(businessCardPre.getId(), "BusinessCardPre", "id", resources.getId());
        businessCardPre.copy(resources);
        businessCardPreRepository.save(businessCardPre);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            businessCardPreRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<BusinessCardPreDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BusinessCardPreDto businessCardPre : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("名片名称", businessCardPre.getName());
            map.put("所属工会", businessCardPre.getAffiliatedTradeUnion());
            map.put("职务", businessCardPre.getPost());
            map.put("地址", businessCardPre.getAddress());
            map.put("联系方式", businessCardPre.getPhone());
            map.put("头像", businessCardPre.getPhoto());
            map.put("部门", businessCardPre.getDept());
            map.put("上级单位", businessCardPre.getSuperiorUnit());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}